import {
	useRequest
} from "./request.js";

import {
	useUserStore
} from "../stores/useUserStore.js";

export const useDishesApis = () => {
	const userStore = useUserStore();

	const request = useRequest();
	const config = {
		header: {
			authorization: userStore.token
		}
	}

	// 查询所有菜品
	function getDishesList() {
		return request.get("/api/dishes", config);
	}

	// 根据日期查询菜单关联菜品 
	function getMenuByDate(date) {
		return request.get(`/api/menus/${date}/dishes`, {
			params: {
				userId: userStore.id || 0
			}
		});
	}

	// 根据分类id查询菜品列表
	function getDishesListByCategoryId(categoryId) {
		return request.get(`/api/categories/${categoryId}/dishes`, config);
	}

	// 根据id查询菜品
	function getDishesById(dishesId) {
		return request.get(`/api/dishes/${dishesId}`, config);
	}

	// 修改菜品
	function updateDishes(dishes) {
		const {
			id,
			...rest
		} = dishes;
		return request.put(`/api/dishes/${id}`, {
			data: rest,
			...config
		});
	}

	// 创建菜品
	function createDish({
		categoryId,
		name,
		description,
		picture,
		thumbnail,
	}) {
		return request.post("/api/dishes", {
			categoryId,
			name,
			description,
			picture,
			thumbnail,
		}, config);
	}

	// 删除菜品
	function deleteDishes(dishesId) {
		return request.delete(`/api/dishes/${dishesId}`, config);
	}

	return {
		getDishesList,
		getMenuByDate,
		getDishesListByCategoryId,
		createDish,
		getDishesById,
		updateDishes,
		deleteDishes,
	}
}